﻿using RBAC.Repository;
using RBAC.Repository.RabbitImpl;
using RBAC.Repository.RabbitInterfae;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Traceab.DTO;
using Traceability.Domain;
using Traceability.Service.Interfae;

namespace Traceability.Service.Impl
{
    public class ImgSrcService: IImgsService
    {
        private readonly IImgRepository _imgRepository;
        /// <summary>
        /// 原料仓储
        /// </summary>
        private readonly IRawRepository _rwRepository;
        /// <summary>
        /// 辅料
        /// </summary>
        private readonly IAuxiliaryRepository _auxiliaryRepository;
        /// <summary>
        /// 添加剂
        /// </summary>
        private readonly IAdditiveRepository _additiveRepository;
        /// <summary>
        /// 包材
        /// </summary>
        private readonly IPackagingRepository _packagingRepository;
        public ImgSrcService(IImgRepository imgRepository, IRawRepository rwRepository, IAuxiliaryRepository auxiliaryRepository,
            IAdditiveRepository additiveRepository, IPackagingRepository packagingRepository)
        {
            _imgRepository = imgRepository;
            _rwRepository = rwRepository;
            _auxiliaryRepository = auxiliaryRepository;
            _additiveRepository = additiveRepository;
            _packagingRepository = packagingRepository;
        }
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="imgs"></param>
        /// <returns></returns>
        public async Task<int> AddImgs(Imgs imgs)
        {
            return await _imgRepository.AddAsync(imgs);
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="ImgName"></param>
        /// <returns></returns>
        public async Task<int> DelTracImg(string? ImgName)
        {
            var res = await _imgRepository.GetAsync(d => d.ImgsName == ImgName);
            foreach (var item in res)
            {
                var dels = await _imgRepository.GetModelsAsync(item.ImgsId);
                await _imgRepository.Delete(dels);
            }
            return 1;
        }
      

       
        /// <summary>
        /// 显示深加工企业信息图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="Deepid"></param>
        /// <returns></returns>
        public async  Task<List<ImgDto>> GetDeepImgAll(string bacth, int pid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          where !a.IsDel && a.DeepEnterprisesId == pid&&a.Batch==bacth
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName
            }).ToList();
            return resimg;
        }

        /// <summary>
        /// 显示养殖企业图片
        /// </summary>
        /// <param name="hid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetEndImgAll(string bacth, int hid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          where !a.IsDel && a.BreedEnterpriseId == hid&&a.Batch==bacth
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName
            }).ToList();
            return resimg;
        }
        /// <summary>
        /// 显示动检证图片
        /// </summary>
        /// <param name="hid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetHesImgAll(string bacth,int hid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          where !a.IsDel && a.HealthId == hid&&a.Batch==bacth
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName
            }).ToList();
            return resimg;
        }


        /// <summary>
        /// 显示产品检验报告图片
        /// </summary>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetImgsAll(string batch, int tid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          where !a.IsDel&& a.Batch == batch&&a.TraceabilityId == tid
                         select a;
            var resimg=listobj.Select(d=>new ImgDto()
            {
                url=d.ImgsName
            }).ToList();
            return resimg;
        }
       

        /// <summary>
        /// 显示深加工食品安全图片
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetPressImgAll(string bacth, int pid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          where !a.IsDel && a.Batch== bacth&&a.ProcessId == pid
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName
            }).ToList();
            return resimg;
        }
        /// <summary>
        /// 显示原料图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetRawImgAll(int tid, int pid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          join b in await _rwRepository.GetAllAsync()
                          on  a.RawId equals b.RawId
                          where !a.IsDel &&b.TraceabilityId==tid&&a.RawId==pid
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName,
                id=d.ImgsId,
                batch=d.Batch
            }).ToList();
            return resimg;
        }
        /// <summary>
        /// 显示辅料图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="Aid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetAuxImgAll(int tid, int pid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          join b in await _auxiliaryRepository.GetAllAsync()
                          on a.AuxiliaryId equals b.AuxiliaryId
                          where !a.IsDel && b.TraceabilityId==tid&& a.AuxiliaryId==pid
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName,
                id = d.ImgsId,
                batch = d.Batch
            }).ToList();
            return resimg;
        }
        /// <summary>
        /// 显示添加剂图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="Additiveid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetAdditiveImgAll(int tid, int pid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          join b in await _additiveRepository.GetAllAsync()
                          on a.AdditiveId equals b.AdditiveId
                          where !a.IsDel &&b.TraceabilityId==tid&& a.AdditiveId==pid
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName,
                id = d.ImgsId,
                batch = d.Batch
            }).ToList();
            return resimg;
        }
        /// <summary>
        /// 显示包材图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="Packagingid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetPackImgAll(int tid, int pid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          join b in await _packagingRepository.GetAllAsync()
                          on a.PackagingId equals b.PackagingId
                          where !a.IsDel && b.TraceabilityId==tid&& a.PackagingId==pid
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName,
                id = d.ImgsId,
                batch = d.Batch
            }).ToList();
            return resimg;
        }
        /// <summary>
        /// 显示屠宰相关资质图片
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetSlImgAll(string bacth, int sid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          where !a.IsDel && a.Batch == bacth&&a.SlaughterId == sid
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName,
                id = d.ImgsId,
                batch = d.Batch
            }).ToList();
            return resimg;
        }
        /// <summary>
        /// 显示屠宰企业图片
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public async Task<List<ImgDto>> GetSlpriseImgAll(string bacth, int pid)
        {
            var listobj = from a in await _imgRepository.GetAllAsync()
                          where !a.IsDel && a.SlaughterEnterPriseId == pid && a.Batch == bacth
                          select a;
            var resimg = listobj.Select(d => new ImgDto()
            {
                url = d.ImgsName
            }).ToList();
            return resimg;
        }
        /// <summary>
        /// 修改相对应的生产批次
        /// </summary>
        /// <param name="imgs"></param>
        /// <returns></returns>
        public async Task<int> UpdateImgs(int Iid, string Xid)
        {

            var ss = await _imgRepository.GetModelsAsync(Iid);
            ss.Batch = Xid;
            return await _imgRepository.UpdateAsync(ss);
        }
    }
}
